//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <numeric>
#include <buola/utility/cunittest.h>
#include <buola/iterator/transform.h>
#include <buola/iterator/indirect.h>
#include <boost/range/adaptors.hpp>

class ASin
{
public:
    typedef double result_type;

    template<typename tType>
    tType operator()(tType pV) const
    {
        return std::abs(pV);
    }
};

using namespace buola;

int main(int pArgC,char **pArgs)
{
    buola_init(pArgC,pArgs);
    
    CUnitTest lTest;

    std::vector<double*> lV;

    for(int i=0;i<50000000;i++)
    {
        lV.push_back(new double(i));
    }

    for(int i=0;i<10;i++)    {
    double lA=0.0;
    start_timer();
    for(int i=0;i<lV.size();i++)
    {
        lA+=std::abs(*lV[i]);
    }
    end_timer();
    msg_info() << lA << "\n";

    lA=0.0;
    start_timer();
    for(double *v : lV)
    {
        lA+=std::abs(*v);
    }
    end_timer();
    msg_info() << lA << "\n";

    lA=0.0;
    start_timer();
    for(double *v : range(lV))
    {
        lA+=std::abs(*v);
    }
    end_timer();
    msg_info() << lA << "\n";

    lA=0.0;
    start_timer();
    for(double v : indirect_range(lV))
    {
        lA+=std::abs(v);
    }
    end_timer();
    msg_info() << lA << "\n";

    lA=0.0;
    start_timer();
    for(double v : transform_range(indirect_range(lV),ASin()))
    {
        lA+=v;
    }
    end_timer();
    msg_info() << lA << "\n";

    msg_info() << "important:\n";
    lA=0.0;
    start_timer();
    for(double v : transform_range(transform_range(lV,fn::ADereference()),ASin()))
    {
        lA+=v;
    }
    end_timer();
    msg_info() << lA << "\n";

    lA=0.0;
    start_timer();
    lA=std::accumulate(transform_iterator(indirect_begin(lV),ASin()),transform_iterator(indirect_end(lV),ASin()),0.0);
    end_timer();
    msg_info() << lA << "\n";

    lA=0.0;
    start_timer();
    for(double v : lV | boost::adaptors::indirected | boost::adaptors::transformed(ASin()))
    {
        lA+=v;
    }
    end_timer();
    msg_info() << lA << "\n";
    }    

    return lTest.ReturnCode();
}
